package com.example.hotel_demo.service;

import com.example.hotel_demo.mapper.UserMapper;
import com.example.hotel_demo.pojo.Page;
import com.example.hotel_demo.pojo.User;
import com.example.hotel_demo.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User getUserByUsername(String username){
        return userMapper.getUserByUsername(username);
    }

    public int updatePasswordByUsername(String username,String password){
        return userMapper.updatePassword(username,password);
    }

    public int updateEmailByUsername(String username,String email){
        return userMapper.updateEmail(username,email);
    }

    //更新用户
    public int updateUser(User user){
        return userMapper.updateUser(user);
    }
    //添加新用户
    public int registUser(User user){
        return userMapper.registUser(user.getUsername(),user.getPassword(),user.getEmail(),user.getRole(),0);
    }

    //根据role字段获得user集合
    public List<User> getUserByRole(int role){
        return userMapper.getUserByRole(role);
    }

    //删除用户
    public int deleteUser(int uID){
        return userMapper.deleteUser(uID);
    }

    public List<User> getUserALL() {
        return userMapper.getUserAll();
    }

    public List<User> getUserALLByEmail(String email) {
        return userMapper.getUserALLByEmail(email);
    }

    public Page pageUsers(String currentPage) {
        int allRowsAmount =userMapper.getAllRowsAmount();

        List<User> UserListAll   = userMapper.getUserAll();

        PageUtil pageUtil =new PageUtil();
        pageUtil.setAllRowsAmount(allRowsAmount);
        if(currentPage !=null){
            pageUtil.setCurrentPage(Integer.parseInt(currentPage));
        }
        pageUtil.calculatePage();

        ArrayList<User> showUsers = new ArrayList<User>();
        int CurrentPageStartRow = pageUtil.getCurrentPageStartRow()-1;
        if(allRowsAmount < (CurrentPageStartRow + pageUtil.getPageSize()) ){
            //总数比访问页面数小
            for(int i = CurrentPageStartRow ; i< allRowsAmount ; i++){
                if(UserListAll.get(i) != null) {
                    showUsers.add(UserListAll.get(i));
                }
            }
        }else{
            for(int i =0;i<pageUtil.getPageSize();i++){
                if(UserListAll.get(CurrentPageStartRow + i) != null) {
                    showUsers.add(UserListAll.get(CurrentPageStartRow + i));
                }
            }
        }

        Page page =new Page();
        page.setPrevPage(pageUtil.getPrevPage());
        page.setNextPage(pageUtil.getNextPage());
        page.setShowUsers(showUsers);
        page.setShowPageNums(pageUtil.getShowPageNums());
        page.setCurrentPage(pageUtil.getCurrentPage());
        return page;
    }

    public int updateUserStateByuid(String uid, String s) {
        return userMapper.updateUserStateByuid(uid,Integer.parseInt(s));
    }

    public User getUserByUid(String uid) {
        return userMapper.getUserByUid(uid);
    }

    public int updateUserByUid(String uID, String username, String password) {
        return userMapper.updateUserByUid(uID,username,password);
    }
}
